package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.common.core.domain.AjaxResult;
import com.wxy.common.utils.ServletUtils;
import com.wxy.common.utils.StringUtils;
import com.wxy.web.model.${entity};
import com.wxy.web.service.I${entity}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
   @Autowired
   private I${entity}Service ${table.entityPath}Service;
   @GetMapping("list")
   public AjaxResult list(${entity} model)
   {
      Integer pageNum = ServletUtils.getParameterToInt("page",1);
      Integer pageSize = ServletUtils.getParameterToInt("limit",10);

      LambdaQueryWrapper<${entity}> lambdaQueryWrapper = Wrappers.lambdaQuery();
      //if(StringUtils.isNotEmpty(model.getName()))
      //{
      //   lambdaQueryWrapper.like(${entity}::getName , model.getName());
      //}

      Page<${entity}> page = new Page<>(pageNum , pageSize);
      page.addOrder(OrderItem.desc("id"));
      IPage<${entity}> iPage = this.${table.entityPath}Service.page(page,lambdaQueryWrapper);

      HashMap<String,Object> map = new HashMap<>();
      map.put("list",iPage.getRecords());
      map.put("count",iPage.getTotal());

      AjaxResult result = AjaxResult.success();
      result.put("data",map);
      return result;
   }


   @GetMapping("info")
   public AjaxResult info(Integer id)
   {
      ${entity} model = ${table.entityPath}Service.getById(id);
      AjaxResult result = AjaxResult.success();
      result.put("data",model);
      return result;
   }

   @PostMapping("add")
   public AjaxResult add(@Validated @RequestBody ${entity} model)
   {
      Boolean success = ${table.entityPath}Service.save(model);
      AjaxResult ajaxResult = null;
      if(success)
      ajaxResult = AjaxResult.success();
      else
      ajaxResult = AjaxResult.error();

      return ajaxResult;
   }

   @PostMapping("edit")
   public AjaxResult edit(@Validated @RequestBody ${entity} model)
   {
      Boolean success = ${table.entityPath}Service.updateById(model);
      AjaxResult ajaxResult = null;
      if(success)
         ajaxResult = AjaxResult.success();
      else
         ajaxResult = AjaxResult.error();
      return ajaxResult;
   }

   @PostMapping("del/{id}")
   public AjaxResult del(@PathVariable Integer id)
   {
      Boolean success = ${table.entityPath}Service.removeById(id);
      AjaxResult ajaxResult = null;
      if(success)
         ajaxResult = AjaxResult.success();
      else
         ajaxResult = AjaxResult.error();
      return ajaxResult;
   }

}
</#if>
